package com.example.forecast.util;

import com.example.forecast.bean.Lunar;
import com.example.forecast.bean.Solar;
import com.example.forecast.data.LiuRenTrigram;

import java.text.SimpleDateFormat;
import java.util.Date;

public class LiuRenUtil {

    private Solar solar;
    private Lunar lunar;
    private int hour;
    private int minute;

    public LiuRenUtil(Solar solar, Lunar lunar, Date date) {
        this.solar = solar;
        this.lunar = lunar;
        hour = Integer.valueOf(new SimpleDateFormat("HH").format(date));
        minute = Integer.valueOf(new SimpleDateFormat("mm").format(date));
    }

    public LiuRenUtil(Solar solar, Lunar lunar, int hour, int minute) {
        this.solar = solar;
        this.lunar = lunar;
        this.hour = hour;
        this.minute = minute;
    }

    /**
     * @author: 日月同珲
     * @time: 2021/11/24 17:30
     * @param: void
     * @effect: 获取八字五行属性分布
     * @return: int property[](五行数组：木，火，土，金，水)
     */
    public int[] getElements() {
        int property[] = new int[]{0, 0, 0, 0, 0};

        String ganZhi = yearPillar().substring(0, 2) + monthPillar().substring(0, 2) + dayPillar().substring(0, 2) + hourPillar().substring(0, 2);


        for (int i = 0; i < 8; i++) {
            String temp = ganZhi.substring(i, i + 1);
            if (temp.equals("甲") || temp.equals("乙") || temp.equals("寅") || temp.equals("卯")) {//木
                property[0]++;
            } else if (temp.equals("丙") || temp.equals("丁") || temp.equals("巳") || temp.equals("午")) {//火
                property[1]++;
            } else if (temp.equals("庚") || temp.equals("辛") || temp.equals("申") || temp.equals("酉")) {//金
                property[3]++;
            } else if (temp.equals("壬") || temp.equals("癸") || temp.equals("子") || temp.equals("亥")) {//水
                property[4]++;
            } else {//土
                property[2]++;
            }
        }

        return property;
    }

    /**
     * @author: 日月同珲
     * @time: 2021/11/23 15:37
     * @param: int state(状态值) 0为农历时间 ， 1为八字
     * @effect: 获取四柱八字
     * @return: String character(四柱八字)
     */
    public String getCharacter(int state) {
        String character = "";

        if (state == 0) {//农历时间
            character = yearPillar() + monthPillar() + dayPillar() + hourPillar();
        } else {//八字
            character = yearPillar().substring(0, 2) + " " +
                    monthPillar().substring(0, 2) + " " +
                    dayPillar().substring(0, 2) + " " +
                    hourPillar().substring(0, 2);
        }

        return character;
    }

    /**
     * @author: 日月同珲
     * @time: 2021/11/24 17:10
     * @param: void
     * @effect: 获取年柱名
     * @return: String yearPillarName(年柱名)
     */
    public String yearPillar() {//年柱

        String yearPillarName = LunarSolarConverter.lunarYearToGanZhi(lunar.lunarYear);//农历年

        return yearPillarName + " ";
    }

    /**
     * @author: 日月同珲
     * @time: 2021/11/11 10:48
     * @param: String gan(年干),int lunarMonth(农历月)
     * @effect: 获取月柱名
     * @return: String monthPillarName(月柱名)
     */
    public String monthPillar() {//月柱

        String monthPillarName = "";

        int january[] = new int[]{0, 2};//正月干支
        String yearGan = yearPillar().substring(0, 1);//年干

        if (yearGan.equals("甲") || yearGan.equals("己")) {
            january[0] = 2;
        } else if (yearGan.equals("乙") || yearGan.equals("庚")) {
            january[0] = 4;
        } else if (yearGan.equals("丙") || yearGan.equals("辛")) {
            january[0] = 6;
        } else if (yearGan.equals("戊") || yearGan.equals("癸")) {
            january[0] = 0;
        } else {
            january[0] = 8;
        }

        int ganIndex = (lunar.lunarMonth + january[0]) % 10;//天干数

        if (ganIndex == 0) {
            ganIndex = 10;
        }
        ganIndex--;//转天干数组下标

        int zhiIndex = (lunar.lunarMonth + 1) % 12;//地支数组下标

        monthPillarName = LiuRenTrigram.tianGan[ganIndex] + LiuRenTrigram.diZhi[zhiIndex].substring(0, 1) + "(" + lunarDate(lunar.lunarMonth, 0) + ")月 ";

        return monthPillarName;
    }

    /**
     * @author: 日月同珲
     * @time: 2021/11/17 22:23
     * @param: int solarYear(阳历年), int dayNum(阳历天数)
     * @effect: 获取日柱名
     * @return: String dayPillarName(日柱名)
     */
    public String dayPillar() {//日柱
        String dayPillarName = "";

        int solarDaySum = 0;//阳历天数

        if (solar.solarMonth < 6) {
            switch (solar.solarMonth) {
                case 1:
                    solarDaySum = 0;
                    break;
                case 2:
                    solarDaySum = 31;
                    break;
                case 3:
                    solarDaySum = 59;
                    break;
                case 4:
                    solarDaySum = 90;
                    break;
                case 5:
                    solarDaySum = 120;
                    break;
                case 6:
                    solarDaySum = 151;
                    break;
            }
        } else {
            switch (solar.solarMonth) {
                case 7:
                    solarDaySum = 181;
                    break;
                case 8:
                    solarDaySum = 212;
                    break;
                case 9:
                    solarDaySum = 243;
                    break;
                case 10:
                    solarDaySum = 273;
                    break;
                case 11:
                    solarDaySum = 304;
                    break;
                case 12:
                    solarDaySum = 334;
                    break;
            }
        }
        solarDaySum += solar.solarDay;
        if (solar.solarYear % 400 == 0 || (solar.solarYear / 4 == 0 && solar.solarYear % 100 != 0) && solar.solarMonth > 2) {//判断闰年二月
            solarDaySum++;
        }

        int temp = ((solar.solarYear - 1900) * 5 + (solar.solarYear - 1900 + 3) / 4 + 9 + solarDaySum) % 60;

        if (temp == 0) {
            temp = 60;
        } else if (hour != 23) {//判断是否为子时
            temp--;//因数组下标起始为0，所以减一
        }

        dayPillarName = LiuRenTrigram.tianGan[temp % 10] + LiuRenTrigram.diZhi[temp % 12].substring(0, 1) + "日" + "(" + lunarDate(lunar.lunarDay, 1) + ") ";

        return dayPillarName;
    }

    /**
     * @author: 日月同珲
     * @time: 2021/11/17 10:57
     * @param: String dayGan(日干) , int hour(阳历小时数) , int minute(阳历分钟数)
     * @effect: 获取时柱名
     * @return: String hourPillarName(时柱名)
     */
    public String hourPillar() {//时柱
        String hourPillarName = "";

        int lunarHourName = 0;
        String dayGan = dayPillar().substring(0, 1);

        if (dayGan.equals("甲") || dayGan.equals("己")) {
            lunarHourName = 0;
        } else if (dayGan.equals("乙") || dayGan.equals("庚")) {
            lunarHourName = 2;
        } else if (dayGan.equals("丙") || dayGan.equals("辛")) {
            lunarHourName = 4;
        } else if (dayGan.equals("丁") || dayGan.equals("壬")) {
            lunarHourName = 6;
        } else if (dayGan.equals("戊") || dayGan.equals("癸")) {
            lunarHourName = 8;
        }

        int temp = minute / 15;
        String quarter = "";
        if (hour % 2 == 1) {
            switch (temp) {
                case 0:
                    quarter = "壹刻";
                    break;
                case 1:
                    quarter = "两刻";
                    break;
                case 2:
                    quarter = "叁刻";
                    break;
                case 3:
                    quarter = "肆刻";
                    break;
            }
        } else {
            switch (temp) {
                case 0:
                    quarter = "伍刻";
                    break;
                case 1:
                    quarter = "陆刻";
                    break;
                case 2:
                    quarter = "柒刻";
                    break;
                case 3:
                    quarter = "捌刻";
                    break;
            }
        }

        hourPillarName = LiuRenTrigram.tianGan[(lunarHourName + lunarHourNum(hour)) % 10] + LiuRenTrigram.lunarHourName[lunarHourNum(hour)] + " " + quarter;

        return hourPillarName;
    }

    /**
     * @author: 日月同珲
     * @time: 2021/11/11 10:53
     * @param: int time(农历月份或日期),int state(状态值)
     * @effect: 转换月份与日期
     * @return: String date(转换后的农历时间)
     */
    private String lunarDate(int time, int state) {//state:0为月，1为日
        String date = "";

        if (state == 0) {

            if (time > 9) {
                date = "拾" + numTransition(time % 10);
            } else {
                date = numTransition(time);
            }

        } else if (state == 1) {
            if (time < 11) {
                if (time == 10) {
                    date = "初拾";
                }
                date = "初" + numTransition(time);
            } else if (time < 20) {
                date = "拾" + lunarDate(time % 10, 1).substring(1);
            } else if (time == 20 || time == 30) {
                date = time > 20 ? "叁拾" : "贰拾";
            } else {
                date = "廿" + lunarDate(time % 10, 1).substring(1);
            }
        }

        return date;
    }

    /**
     * @author: 日月同珲
     * @time: 2021/11/11 10:51
     * @param: int singular
     * @effect: 将数字转换为中文数字
     * @return: String num(中文数字)
     */
    private String numTransition(int singular) {
        String num = "";

        switch (singular) {
            case 0:
                break;
            case 1:
                num = "壹";
                break;
            case 2:
                num = "贰";
                break;
            case 3:
                num = "叁";
                break;
            case 4:
                num = "肆";
                break;
            case 5:
                num = "伍";
                break;
            case 6:
                num = "陆";
                break;
            case 7:
                num = "柒";
                break;
            case 8:
                num = "捌";
                break;
            case 9:
                num = "玖";
                break;
        }

        return num;
    }

    /**
     * @author: 日月同珲
     * @time: 2021/11/10 16:45
     * @param: int hour(小时)
     * @effect: 获取时辰
     * @return: int hourName(时辰数组下标)
     */
    private int lunarHourNum(int hour) {
        int hourName;

        if (hour == 23 || hour == 0) {//子时
            hourName = 0;
        } else {
            if (hour < 11) {//丑时至巳时
                if (hour < 5) {//丑时至寅时
                    if (hour < 3 && hour > 0) {//丑时
                        hourName = 1;
                    } else {//寅时
                        hourName = 2;
                    }
                } else {//卯时至巳时
                    if (hour < 7) {//卯时
                        hourName = 3;
                    } else if (hour > 8) {//巳时
                        hourName = 5;
                    } else {//辰时
                        hourName = 4;
                    }
                }
            } else {//午时至亥时
                if (hour < 17) {//午时至申时
                    if (hour < 13) {//午时
                        hourName = 6;
                    } else if (hour > 14) {//申时
                        hourName = 8;
                    } else {//未时
                        hourName = 7;
                    }
                } else {//酉时至亥时
                    if (hour < 19) {//酉时
                        hourName = 9;
                    } else if (hour > 20) {//亥时
                        hourName = 11;
                    } else {//戌时
                        hourName = 10;
                    }
                }
            }
        }
        return hourName;
    }
}
